package org.jeecg.modules.luban.service.impl;

import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.luban.entity.PlanWorkplan;
import org.jeecg.modules.luban.entity.PlanComplete;
import org.jeecg.modules.luban.mapper.PlanCompleteMapper;
import org.jeecg.modules.luban.mapper.PlanWorkplanMapper;
import org.jeecg.modules.luban.service.IPlanWorkplanService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 工作计划
 * @Author: jeecg-boot
 * @Date:   2020-05-27
 * @Version: V1.0
 */
@Service
public class PlanWorkplanServiceImpl extends ServiceImpl<PlanWorkplanMapper, PlanWorkplan> implements IPlanWorkplanService {

	@Autowired
	private PlanWorkplanMapper planWorkplanMapper;
	@Autowired
	private PlanCompleteMapper planCompleteMapper;
	
	@Override
	@Transactional
	public void saveMain(PlanWorkplan planWorkplan, List<PlanComplete> planCompleteList,String userId) {
		planWorkplanMapper.insert(planWorkplan);
		if(planCompleteList!=null && planCompleteList.size()>0) {
			for(PlanComplete entity:planCompleteList) {
				//外键设置
				entity.setWorkplanId(planWorkplan.getId());
				entity.setUserId(userId);
				entity.setWorkDate(DateUtils.formatDate());
				planCompleteMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(PlanWorkplan planWorkplan,List<PlanComplete> planCompleteList) {
		planWorkplanMapper.updateById(planWorkplan);
		
		//1.先删除子表数据
		planCompleteMapper.deleteByMainId(planWorkplan.getId());
		
		//2.子表数据重新插入
		if(planCompleteList!=null && planCompleteList.size()>0) {
			for(PlanComplete entity:planCompleteList) {
				//外键设置
				entity.setWorkplanId(planWorkplan.getId());
				planCompleteMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		planCompleteMapper.deleteByMainId(id);
		planWorkplanMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			planCompleteMapper.deleteByMainId(id.toString());
			planWorkplanMapper.deleteById(id);
		}
	}
	
}
